
$(function () {
    queryYearthList("yearAnnual");
    queryDepartmentTree();
    var TRAIN_PLANTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_PLANTYPE;//计划分类标识
    queryComboData(TRAIN_PLANTYPE,'planTypeId');
    var TRAIN_TRAINTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_TRAINTYPE;//培训类别标识
    queryComboData(TRAIN_TRAINTYPE,'trainTypeId');
    var TRAIN_PROTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_PROTYPE;//专业类别标识
    queryComboData(TRAIN_PROTYPE,'professionalTypeId');
    var TRAIN_LEVEL=CONSTANT.DictionaryTypeDefConstants.TRAIN_LEVEL;//培训级别标识
    queryComboData(TRAIN_LEVEL,'trainLevelId');
    planStartMonth();
    var TRAIN_ZSTYPE=CONSTANT.DictionaryTypeDefConstants.TRAIN_ZSTYPE;//证书类型(数据字典)
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: TRAIN_ZSTYPE},
        dataType: "json",
        success: function (result) {
            $('#certificateTypeId').combobox({
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
                multiple:true
            });
        }
    });
    $('#certificateTypeId').val(certificateTypeIdsValue.substring(1));
    $('#btn_save').on('click', function () {
        submitForm();
    });
    /**
     * 绑定人员选择事件
     */
    $("#principal").textbox({
        iconWidth: 20,
        icons: [{
            iconCls: 'icon-add',
            handler: function(e){
                add_personLiable_func();
            }
        },{
            iconCls: 'icon-remove',
            handler: function(e){
                $(e.data.target).textbox('clear');
                $("#principalName").val('');
            }
        }]
    });
    
    $("input[name='isExternal']").bind("click",function(){
        if ($(this).val()=='1'){
            $("#deptDiv2").show();
            $("#hostDeptId").combobox({
                required:false,
                editable: false
            });
            $("#hostDeptName").textbox({
                required:true
            });
            $("#deptDiv1").hide();
        }else {
            $("#deptDiv1").show();
            $("#hostDeptId").combobox({
                required:true,
                editable: false
            });
            $("#hostDeptName").textbox({
                required:false
            });
            $("#deptDiv2").hide();
        }
        $("#hostDeptId").combobox('setValue','');
        $("#hostDeptName").val('');
    });
    $("input[name='hasCertificate']").bind("click",function(){
        if ($(this).val()=='1'){
            $("#certificateTypeId").combobox({
                required:true,
                editable: false,
                disabled:false
            });
        }else {
            $("#certificateTypeId").combobox({
                required:false,
                editable: false,
                disabled:true
            });
            $("#certificateTypeId").val('');
        }
    });
});
function queryYearthList(id){
    var date = new Date();
    var year = date.getFullYear();
    var yearthData = new Array();
    //yearthData.push({id:"",text:"请选择"});
    for(var i =0;i<6;i++){
        var y = {id:year-i,text:(year-i)+'年'}
        yearthData.push(y);
    }
    $('#'+id).combobox(
        {
            data:yearthData,
            prompt:'请选择',
            valueField:'id',
            textField:'text',
            required:true
        }
    );
}
function planStartMonth() {
    $('#planStartMonth').datebox({
        required:true,
        onShowPanel: function () {//显示日趋选择对象后再触发弹出月份层的事件，初始化时没有生成月份层
            span.trigger('click'); //触发click事件弹出月份层
            if (!tds) setTimeout(function () {//延时触发获取月份对象，因为上面的事件触发和对象生成有时间间隔
                tds = p.find('div.calendar-menu-month-inner td');
                tds.click(function (e) {
                    e.stopPropagation(); //禁止冒泡执行easyui给月份绑定的事件
                    var year = /\d{4}/.exec(span.html())[0]//得到年份
                        , month = parseInt($(this).attr('abbr'), 10); //月份，这里不需要+1
                    $('#planStartMonth').datebox('hidePanel')//隐藏日期对象
                        .datebox('setValue', year + '-' + month); //设置日期的值
                });
            }, 0);
            yearIpt.unbind();//解绑年份输入框中任何事件
        },
        parser: function (s) {
            if (!s) return new Date();
            var arr = s.split('-');
            return new Date(parseInt(arr[0], 10), parseInt(arr[1], 10) - 1, 1);
        },
        formatter: function (d) {
            var a = parseInt(d.getMonth())<parseInt('9')?'0'+parseInt(d.getMonth()+ 1):d.getMonth() + 1;
            return d.getFullYear() + '-' +a; }
    });
    var p = $('#planStartMonth').datebox('panel'), //日期选择对象
        tds = false, //日期选择对象中月份
        yearIpt = p.find('input.calendar-menu-year'),//年份输入框
        span = p.find('span.calendar-text'); //显示月份层的触发控件
}
/**
 * 查询部门树方法
 */
function queryDepartmentTree(){
    $.ajax({
        url:ROOT_PATH + '/permission/department/getDepartmentTree.shtml',//提交地址
        data:{parentId : 0},//将表单数据序列化
        type:"POST",
        dataType:"json",
        success:function(result){
            $('#hostDeptId').combotree(
                {
                    data:result,
                    prompt:'请选择',
                    require:true
                }
            );
        }
    });
}

var add_personLiable_func = function () {
    WindowManager.openLayer({
        type: 2,
        title: '选择人员信息',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
        callBack:function(data){
            getSelectedUser(data);
        }
    });
};
/**
 * 人员信息选择
 */
var getSelectedUser = function (data) {
    $("#principal").textbox('setValue', data[0].id).textbox('setText', data[0].realName);
    $("#principalName").val(data[0].id);
};

function submitForm() {
    $('#formPlan').form('submit', {
        url: ROOT_PATH + '/train/plan/updateByIdSelective.shtml',
        onSubmit: function (param) {
            if (!$(this).form('validate')) {
                layer.alert('请正确填写信息', {icon: 5});
            }
            return $(this).form('validate');
        },
        success: function (result) {
            result = $.parseJSON(result);
            if (result.success) {
                layer.msg('保存成功', {
                    icon: 1,
                    time: 1000 //2秒关闭（如果不配置，默认是3秒）
                }, function () {
                    WindowManager.closeLayer({
                        returnValue: true
                    });
                });
            }
            else {
                layer.msg('保存失败', {
                    icon: 2,
                    time: 1000
                });
            }
        }
    });
}
function queryComboData(code,queryId) {
    /*var EMPTY_OPTION = {
     id: '',
     text: '请选择'
     };*/
    var pageId='#'+queryId;
    $.ajax({
        type: 'post',
        url: ROOT_PATH + '/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml',
        data: {code: code},
        dataType: "json",
        success: function (result) {
            $(pageId).combobox({
                /*loadFilter: function (data) {
                 data.unshift(EMPTY_OPTION);
                 return data;
                 },*/
                data: result,
                prompt:'请选择',
                valueField: 'id',
                textField: 'text',
                editable: false,
                panelHeight: '200',
            });
        }
    });
}